<template>
    <div class="login-page">
        <!-- 背景装饰 -->
        <div class="bg-decoration">
            <div class="circle circle-1"></div>
            <div class="circle circle-2"></div>
            <div class="circle circle-3"></div>
        </div>

        <!-- 登录卡片 -->
        <div class="login-card">
            <!-- 头部 -->
            <div class="login-header">
                <div class="logo-container">
                    <img src="/src/assets/logo.svg" alt="logo" class="logo">
                </div>
                <h1 class="title">文章中心</h1>
                <p class="subtitle">欢迎回来，请登录您的账户</p>
            </div>

            <!-- 登录表单 -->
            <el-form :model="loginForm" ref="loginFormRef" :rules="rules" class="login-form">
                <el-form-item prop="username" class="form-item">
                    <el-input v-model="loginForm.username" placeholder="请输入账号" clearable class="custom-input"
                        size="large">
                        <template #prefix>
                            <el-icon class="input-icon">
                                <User />
                            </el-icon>
                        </template>
                    </el-input>
                </el-form-item>

                <el-form-item prop="password" class="form-item">
                    <el-input v-model="loginForm.password" placeholder="请输入密码" clearable type="password" show-password
                        class="custom-input" size="large">
                        <template #prefix>
                            <el-icon class="input-icon">
                                <Lock />
                            </el-icon>
                        </template>
                    </el-input>
                </el-form-item>

                <el-form-item class="form-item">
                    <el-button type="primary" @click="login(loginFormRef)" class="login-button" size="large"
                        @keyup.enter="login(loginFormRef)" v-loading="loading">
                        <span v-if="!loading">登录</span>
                        <span v-else>登录中...</span>
                    </el-button>
                </el-form-item>

            </el-form>
        </div>


    </div>
</template>

<script setup lang="ts">
import { ElMessage, type FormInstance, type FormRules } from 'element-plus'
import { reactive, ref } from 'vue'
import { useAuthStore } from '../store/auth'
import { useRouter } from 'vue-router'
import service from '../config'

const authStore = useAuthStore()
const router = useRouter()
const loading = ref(false)
const loginFormRef = ref<FormInstance>()


interface RuleForm {
    username: string
    password: string
}


const loginForm = ref<RuleForm>({
    username: '',
    password: ''
})

const rules = reactive<FormRules<RuleForm>>({
    username: [
        { required: true, message: '请输入账号', trigger: 'blur' },
    ],
    password: [
        { required: true, message: '请输入密码', trigger: 'blur' },
    ],
})


const login = async (formEl: FormInstance | undefined) => {
    if (!formEl) return
    await formEl.validate(async (valid, fields) => {
        if (valid) {
            loading.value = true
            const success = await authStore.login(loginForm.value)
            console.log(success);

            if (success) {
                ElMessage.success('登录成功')
                loading.value = false
                const redirect = router.currentRoute.value.query.redirect
                router.push(redirect ? redirect.toString() : { name: 'Article' })
            } else {
                loading.value = false
            }
        }
    })
}

</script>

<style lang="scss" scoped>
.login-page {
    min-height: 100vh;
    background: linear-gradient(135deg, #35b1b1 0%, #2a8a8a 100%);
    display: flex;
    justify-content: center;
    align-items: center;
    position: relative;
    overflow: hidden;
    padding: 20px;
}

// 背景装饰圆圈
.bg-decoration {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    pointer-events: none;
    z-index: 1;
}

.circle {
    position: absolute;
    border-radius: 50%;
    background: rgba(255, 255, 255, 0.08);
    backdrop-filter: blur(10px);
    animation: float 6s ease-in-out infinite;
    border: 1px solid rgba(53, 177, 177, 0.1);
}

.circle-1 {
    width: 200px;
    height: 200px;
    top: 10%;
    left: 10%;
    animation-delay: 0s;
}

.circle-2 {
    width: 150px;
    height: 150px;
    top: 60%;
    right: 15%;
    animation-delay: 2s;
}

.circle-3 {
    width: 100px;
    height: 100px;
    bottom: 20%;
    left: 20%;
    animation-delay: 4s;
}

@keyframes float {

    0%,
    100% {
        transform: translateY(0px) rotate(0deg);
    }

    50% {
        transform: translateY(-20px) rotate(180deg);
    }
}

// 登录卡片
.login-card {
    background: rgba(255, 255, 255, 0.95);
    backdrop-filter: blur(20px);
    border-radius: 20px;
    padding: 40px;
    box-shadow: 0 20px 40px rgba(53, 177, 177, 0.15);
    width: 100%;
    max-width: 420px;
    position: relative;
    z-index: 2;
    border: 1px solid rgba(53, 177, 177, 0.1);
}

// 登录头部
.login-header {
    text-align: center;
    margin-bottom: 40px;
}

.logo-container {
    margin-bottom: 20px;
}

.logo {
    width: 60px;
    height: 60px;
    object-fit: contain;
}

.title {
    font-size: 28px;
    font-weight: 700;
    color: #2c3e50;
    margin: 0 0 10px 0;
    background: linear-gradient(135deg, #35b1b1, #2a8a8a);
    -webkit-background-clip: text;
    -webkit-text-fill-color: transparent;
    background-clip: text;
}

.subtitle {
    color: #7f8c8d;
    font-size: 14px;
    margin: 0;
    font-weight: 400;
}

// 登录表单
.login-form {
    .form-item {
        margin-bottom: 24px;

        &:last-child {
            margin-bottom: 0;
        }
    }
}

// 登录页脚
.login-footer {
    text-align: center;
    margin-top: 20px;
}

.change-password-link {
    color: #35b1b1;
    font-size: 14px;
    padding: 0;

    &:hover {
        color: #2a8a8a;
    }
}

// 修改密码对话框按钮样式
.change-password-submit-btn {
    background: linear-gradient(135deg, #35b1b1 0%, #2a8a8a 100%) !important;
    border: none !important;
    box-shadow: 0 4px 15px rgba(53, 177, 177, 0.3) !important;

    &:hover {
        background: linear-gradient(135deg, #3bc4c4 0%, #319494 100%) !important;
        box-shadow: 0 8px 25px rgba(53, 177, 177, 0.4) !important;
        transform: translateY(-2px);
    }

    &:active {
        transform: translateY(0);
    }

    &.is-loading {
        background: linear-gradient(135deg, #35b1b1 0%, #2a8a8a 100%) !important;
    }
}

// 自定义输入框样式
:deep(.custom-input) {
    .el-input__wrapper {
        border-radius: 12px;
        border: 2px solid #e1e8ed;
        background: #f8f9fa;
        box-shadow: none;
        transition: all 0.3s ease;
        padding: 0 16px;
        height: 50px;

        &:hover {
            border-color: #35b1b1;
            background: #fff;
        }

        &.is-focus {
            border-color: #35b1b1;
            background: #fff;
            box-shadow: 0 0 0 3px rgba(53, 177, 177, 0.1);
        }
    }

    .el-input__inner {
        color: #2c3e50;
        font-size: 15px;
        font-weight: 500;

        &::placeholder {
            color: #95a5a6;
            font-weight: 400;
        }
    }

    .input-icon {
        color: #35b1b1;
        font-size: 18px;
    }
}

// 登录按钮
.login-button {
    width: 100%;
    height: 50px;
    border-radius: 12px;
    background: linear-gradient(135deg, #35b1b1 0%, #2a8a8a 100%);
    border: none;
    font-size: 16px;
    font-weight: 600;
    letter-spacing: 0.5px;
    transition: all 0.3s ease;
    box-shadow: 0 4px 15px rgba(53, 177, 177, 0.3);

    &:hover {
        transform: translateY(-2px);
        box-shadow: 0 8px 25px rgba(53, 177, 177, 0.4);
        background: linear-gradient(135deg, #3bc4c4 0%, #319494 100%);
    }

    &:active {
        transform: translateY(0);
    }

    &.is-loading {
        background: linear-gradient(135deg, #35b1b1 0%, #2a8a8a 100%);
    }
}

// 响应式设计
@media (max-width: 480px) {
    .login-page {
        padding: 15px;
    }

    .login-card {
        padding: 30px 25px;
        border-radius: 15px;
    }

    .title {
        font-size: 24px;
    }

    .circle-1,
    .circle-2,
    .circle-3 {
        display: none;
    }
}

// 加载动画优化
:deep(.el-loading-spinner) {
    .circular {
        width: 24px;
        height: 24px;
    }
}

// 表单验证样式优化
:deep(.el-form-item__error) {
    color: #e74c3c;
    font-size: 12px;
    font-weight: 500;
}

:deep(.el-form-item.is-error) {
    .el-input__wrapper {
        border-color: #e74c3c;
        box-shadow: 0 0 0 3px rgba(231, 76, 60, 0.1);
    }
}
</style>